/**
 * TODO
 */
package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.FgdzzMapper;
import com.bci.pwtz.mysql.model.Fgdzz;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.FgdzzService;

/**
 * TODO
 * 
 * @author ICLNetwork Team
 * @version 3.0
 */
@Service
public class FgdzzServiceImpl implements FgdzzService{

    private FgdzzMapper objMapper;
    
    private CompanyService companyService;
    
    private BusinessRefService businessRefService;
    
    private static final Logger _logger = Logger.getLogger(PdbyqServiceImpl.class);

    public FgdzzMapper getObjMapper()
    {
        return objMapper;
    }

    public void setObjMapper(FgdzzMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    public CompanyService getCompanyService() {
        return companyService;
    }

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    

    public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }

    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }

    /**
     * 新增反孤岛装置信息
     */
    @Override
    public Fgdzz add(Fgdzz fgdzz) throws PwtzDatabaseException {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:fgdzz=" + fgdzz.toString());
        }
        if (fgdzz == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,fgdzz is null");
            }
            return null;
        }else{
//            UUID uuid = UUID.randomUUID();
//            fgdzz.setDeviceid(uuid.toString());
            fgdzz.setStatus(1);
            fgdzz.setLastmodifiedtime(new Date());
            fgdzz.setCreatedate(new Date());
        }
        try
        {
            objMapper.insertSelective(fgdzz);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return fgdzz;
    }

    /**
     * 删除反孤岛装置信息
     */
    @Override
    public int delete(String fgdzzId) throws PwtzDatabaseException, CanNotDeleteException {
        int result=0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("delete:fgdzzId=" + fgdzzId);
        }
        try
        {
            boolean bl = businessRefService.canDelete("T_FGDZZ", fgdzzId);
            if(!bl){
                throw new CanNotDeleteException();
            }
            result = objMapper.deleteById(fgdzzId,new Date());
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /** 
     * 修改反孤岛装置信息
     */
    @Override
    public int update(Fgdzz fgdzz) throws PwtzDatabaseException {
        int result=0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("update:fgdzz=" + fgdzz.toString());
        }
        if (fgdzz == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("update,fgdzz is null");
            }
            return 0;
        }else{
            fgdzz.setLastmodifiedtime(new Date());
        }
        try
        {
            result = objMapper.updateByPrimaryKeySelective(fgdzz);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /**
     * 反孤岛装置的load方法
     */
    @Override
    public Fgdzz load(String fgdzzId) throws PwtzDatabaseException {
        if (_logger.isInfoEnabled())
        {
            _logger.info("load:FgdzzId=" + fgdzzId);
        }
        Fgdzz fgdzz = null;
        try
        {
            fgdzz = objMapper.selectByPrimaryKey(fgdzzId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (fgdzz != null)
        {
            _logger.debug("load:result fgdzz=" + fgdzz.toString());
        }
        return fgdzz;
    }

    /**
     * 反孤岛装置的list方法
     */
    @Override
    public PaginationList<Fgdzz> findFgdzz(String devicename, Long companyid, int portalType,String[] medLineCodes,int stationDeviceType,String stationCode, String beginTime,
            String endTime, int pageSize, int pageNo) throws PwtzDatabaseException {
      PaginationList<Fgdzz> paginationList = new PaginationList<Fgdzz>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("findDybx:|devicename=" + devicename+"|beginTime=" + beginTime+"|endTime=" + endTime+"|companyid="+companyid+"|portalType="+portalType+"|medLineCodes="+PwtzUtils.parseCharacterArrayToString(medLineCodes)+"|stationDeviceType="+stationDeviceType+"|stationCode="+stationCode+"|pageSize="+pageSize+"|pageNo="+pageNo);
        }
        List<Fgdzz> list = null;
        int beginIndex = pageSize * pageNo - pageSize;
        String companyidStr = "";
        String[] companyids=null;
        if(portalType==PwtzConstants.PORTAL_TYPE_WEB){
            companyidStr = companyService.findChildrensId(companyid,null);
            if(companyidStr!=null && companyidStr.length()>0){
                companyidStr= companyidStr.substring(0,companyidStr.length()-1);
                companyids =companyidStr.split(",");
            }
        }
        try
        {
            list = objMapper.selectDeviceList(devicename,companyid,companyids,portalType,medLineCodes,stationDeviceType,stationCode, beginTime,endTime,beginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findFgdzz:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = objMapper.selectDeviceCount(devicename,companyid,companyids,portalType,medLineCodes,stationDeviceType,stationCode, beginTime,endTime);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }

    @Override
    public PaginationList<Fgdzz> selectDevice(String devicename, Long companyid, String stationcode, int pagesize ,int pageno) throws PwtzDatabaseException
    {
PaginationList<Fgdzz> paginationList = new PaginationList<Fgdzz>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("findDybx:|devicename=" + devicename+"|companyid="+companyid+"|stationCode="+stationcode+"|pageSize="+pagesize+"|pageNo="+pageno);
        }
        List<Fgdzz> list = null;
        int beginIndex = pagesize * pageno - pagesize;
        try
        {
            list = objMapper.selectDevice(devicename,companyid,stationcode,beginIndex, pagesize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findFgdzz:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pagesize!=0){
            try
            {
                totalCount = objMapper.selectCount(devicename,companyid,stationcode);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pagesize == 0 ? totalCount / pagesize : totalCount / pagesize + 1;
        }else{
            pageno=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageno);
        paginationList.setPageSize(pagesize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
}
